import Vue from 'vue'
import store from '@/store/store'
const that = Vue.prototype
const xin = {
		configData:{
				loadText: {
					loadmore: '点击加载更多',
					loading: '努力加载中',
					nomore: '没有更多数据了',
				},
				// #ifdef H5
				baseURL: '/api', //baseURL/
				// #endif
				// #ifdef MP-WEIXIN
				baseURL: 'http://willmid.com', //baseURL
				// #endif
				imgUrl:'',

		},
		wxLogin(redirect_uri){
			redirect_uri = redirect_uri || xin.configData.redirect_uri
			window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + xin.configData.wxConfig.appid + "&redirect_uri=" + redirect_uri + "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
		},
		backPage:()=>{
			// 返回页面
			let pages = getCurrentPages()
			if (pages.length === 1) {
				xin.goUrl('/pages/index/index')
			} else {
				uni.navigateBack({
					//uni.navigateTo跳转的返回，默认1为返回上一级
					delta: 1
				});
			}
		},
		LoaDing:(title)=>{
			uni.showLoading({
				title: title || '加载中',
				mask:true,
				fail:(err)=>{
					console.log(err)
				}
			});
		},
    Toast:(title,flag) => {
			uni.showToast({
				title,
				icon:  flag ? 'success' : 'none',
			})
		},
		goUrl:(url,type) => {
				let urltype = type ? 'switchTab' : 'navigateTo'
				if (url) {
					uni[urltype]({
						url: `${url}`,
						success: () => {
							store.navFlag  = false
						},
						fail: (err) => {
							console.log(err);
						}
					})
				} else {
					uni.showToast({
						title: "链接无效",
						icon: 'none'
					})
				}
			},
		previewImage: (imgList, index) => {
				if (!imgList) {
					this.Toast('查看图片错误')
					return false
				}
				index || 0
				// 预览图片
				uni.previewImage({
					current: index || 0,
					urls: imgList,
					indicator: 'number',
					fail: (err) => {
						console.log(err);
					},
					longPressActions: {
						success: function(data) {},
						fail: function(err) {
							console.log(err.errMsg);
						}
					}
				});
			},
			async uplodaImg(tempFilePaths){
				return new Promise((reslove,riject)=>{
					// setTimeout(()=>{
					// 	reslove(5000)
					// },1500)
					setTimeout(()=>{
						uni.uploadFile({
							url: this.configData.baseURL+'/files/upload_files', //仅为示例，非真实的接口地址
							filePath: tempFilePaths[0],
							name: 'files',
							success: (uploadFileRes) => {
								reslove(JSON.parse(uploadFileRes.data))
							},
							fail:(err)=>{
								riject(err)
							}
						});
					},500)
			
				})

			},
		
}
export default xin